/************插入图片***********/
function imageChange(item){
    var img=document.getElementById("previewImage");
	var preview=document.getElementById("previewButton");
    if(item.files.length === 1){
        var url=window.URL.createObjectURL(item.files[0]);
        img.src=url;
        var uVideo = document.getElementById("uploadVideo");
        if(uVideo.files.length === 1){
            preview.style.visibility="visible";
        }
    }else if(item.files.length === 0){
        img.src="../resources/images/uploadImage.png";
        preview.style.visibility="hidden";
    }
}

/************插入视频*********/
function videoChange(item){
    var vid=document.getElementById("previewVideo");
	var preview=document.getElementById("previewButton");
    if(item.files.length === 1){
        var url=window.URL.createObjectURL(item.files[0]);
        vid.style.backgroundImage="url('../resources/images/load.gif')";
        vid.src = url;
        var uImage = document.getElementById("uploadImage");
        if(uImage.files.length === 1){
            preview.style.visibility="visible";
        }
    }else if(item.files.length === 0){
        vid.src="";
        preview.style.visibility="hidden";
    }
}

/************重置图片视频封面*********/
function resett(){
    var img=document.getElementById("previewImage");
	img.src="../resources/images/uploadImage.png";
	var vid=document.getElementById("previewVideo");
	vid.style.backgroundImage="url('../resources/images/uploadVideo.png')";
	vid.src="";
    document.getElementById("previewButton").style.visibility="hidden";
}

/************图片视频预览*********/
function showIV(){
    var IV=document.getElementById("IVpreview");
	var uImage = document.getElementById("uploadImage");
	var urlOne=window.URL.createObjectURL(uImage.files[0])
	var uVideo = document.getElementById("uploadVideo");
	var urlTwo=window.URL.createObjectURL(uVideo.files[0]);
	IV.style.backgroundImage="url("+urlOne+")";
	IV.src=urlTwo;
}

/************获取项目根路径***********/
function getRootPath_web() {
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    let curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    let pathName = window.document.location.pathname;
    let pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    let localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    let projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}

/**
 * 上传视频相关属性值
 * @param form
 */
// function sendVideoAttribute(form){
//     //let basePath = getRootPath_web(), dat;
//     $.ajax({
//         url:getRootPath_web()+"/video/upload",
//         type:"post",
//         dataType: "json",
//         contentType: "application/json",
//         data:JSON.stringify(form),
//         success:function(data){
//             //$('#reset').click();
//             uploadStatus(false);
//             confirm("上传成功");
//         },
//         error:function(e){
//             console.log(e);
//             //$('#reset').click();
//             uploadStatus(false);
//             alert("上传出错4");
//         }
//     });
// }

// 版本二（箭头语法）
// var convert_FormData_to_json2 = function (formData) {
//     var objData = {};
//
//     formData.forEach((value, key) => objData[key] = value);
//
//     return JSON.stringify(objData);
// };

/***********发送表单数据*********/
function sendFormData(){
    let current_user = get_current_user();
    if(current_user.user_id == 0){
        alert("登录后才可以上传");
        return false;
    }
    //视频时长
    var vid=document.getElementById("previewVideo");
    document.getElementById("videoTime").value=vid.duration;
    document.getElementById("userId").value=current_user.user_id;
    uploadStatus(true);
    //发送到后台处理
    var form = new FormData(document.getElementById("formData"));
    //console.log(convert_FormData_to_json2(form))
    $.ajax({
        url:getRootPath_web()+"/video/upload",
        type:"post",
        data:form,
        mimeType: "multipart/form-data",
        processData:false,
        contentType:false,
        // dataType:"json",
        success:function(data){
            uploadStatus(false);
            confirm("上传成功");
        },
        error:function(e){
            //$('#reset').click();
            uploadStatus(false);
            alert("上传出错1");
        }
    });
    return false;
}

/**
 * 关闭或显示加载动画
 * @param note
 */
function uploadStatus(note){
    var uf=document.getElementById("uploadFlash");
    if(note){
        uf.style.display="block";
    }else{
        $('#reset').click();
        uf.style.display="none";
    }
}

/**
 * 当输入框只有空格时消空格
 * @param item
 */
function setEmpty(item){
	if(item.value.trim()==''){
		item.value='';
		var stringLength = item.parentNode.children[1];
		if(stringLength.id=="titleLength")
			stringLength.innerHTML="0/28";
		else if(stringLength.id=="describeLength")
			stringLength.innerHTML="0/200";
	}
	var border = item.parentNode;
	border.style.boxShadow="0px 0px 0px #ffffff";
	border.style.borderColor="Gray";
	border.children[1].style.color="LightGray";
}

/**
 * 显示输入字符长度的变化
 * @param item
 */
function showStringLength(item){
	var stringLength = item.parentNode.children[1];
	if(stringLength.id=="titleLength")
		stringLength.innerHTML=item.value.length+"/28";
	else if(stringLength.id=="describeLength")
	stringLength.innerHTML=item.value.length+"/200";
}

function blueBorder(item){
	var border = item.parentNode;
	border.style.boxShadow="0px 0px 5px #51cbee";
	border.style.borderColor="#35a5e5";
	border.children[1].style.color="Gray";
}


